package org.malred.hotelsystem.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.admin.HotelDetailActivity;
import org.malred.hotelsystem.model.entity.HotelEntity;

import java.util.List;

public class HotelRecycleAdapter extends RecyclerView.Adapter<HotelRecycleAdapter.Holder> {
    List<HotelEntity> items;
    Context context;
    // 它的作用类似于 findViewById()
    LayoutInflater inflater;

    public HotelRecycleAdapter(Context context, List<HotelEntity> items) {
        this.context = context;
        this.items = items;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 每个item的xml布局
        View view = inflater.inflate(R.layout.manage_list_item_layout, parent, false);

        // 可以在这给holder(单个list项)注册点击
        Holder holder = new Holder(view);
        return holder;
    }

    // 给holder里的控件赋值
    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        HotelEntity hotelEntity = items.get(position);
        holder.tvTitle.setText(hotelEntity.getTitle());
        holder.tvContent.setText(hotelEntity.getDescription());
        holder.img.setImageResource(R.drawable.cover);
        holder.id = hotelEntity.getId();
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView img;
        TextView tvTitle;
        TextView tvContent;
        String id;

        public Holder(@NonNull View itemView) {
            super(itemView);

            this.img = itemView.findViewById(R.id.iv_img);
            this.tvTitle = itemView.findViewById(R.id.tv_title);
            this.tvContent = itemView.findViewById(R.id.tv_content);

            img.setOnClickListener(this);
            tvTitle.setOnClickListener(this);
            tvContent.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            Intent in = new Intent(context, HotelDetailActivity.class);
            in.putExtra("hotel_id", id);
            context.startActivity(in);
        }
    }
}

